﻿using CloudDcPc.Entity;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CloudDcPc.FuckDlg
{
    /// <summary>
    /// MenuAddDlg.xaml 的交互逻辑
    /// </summary>
    public partial class MyMenuAddDlg : Window
    {
        ObservableCollection<MyMenu> all_MyMenu = null;
        public MyMenuAddDlg(ObservableCollection<MyMenu> all_MyMenu)
        {
            this.all_MyMenu = all_MyMenu;
            InitializeComponent();
            try
            {
                string[] arr_food_unit = Properties.Settings.Default.save_food_unit.Split('-');
                cb_food_unit.ItemsSource = arr_food_unit;
                if (arr_food_unit.Length == 0)
                    cb_food_unit.Visibility = Visibility.Collapsed;
            }
            catch
            {
                MessageBox.Show("设置中菜品单位配置有误！");
                cb_food_unit.Visibility = Visibility.Collapsed;
            }
            try
            {
                string[] arr_food_type = Properties.Settings.Default.save_food_type.Split('-');
                cb_food_type.ItemsSource = arr_food_type;
                if (arr_food_type.Length == 0)
                    cb_food_type.Visibility = Visibility.Collapsed;
            }
            catch
            {
                MessageBox.Show("设置中菜品类别配置有误！");
                cb_food_type.Visibility = Visibility.Collapsed;
            }
            try
            {
                string[] arr_suffix = Properties.Settings.Default.save_food_suffix.Split('-');
                cb_suffix.ItemsSource = arr_suffix;
                if (arr_suffix.Length == 0)
                    cb_suffix.Visibility = Visibility.Collapsed;
            }
            catch
            {
                MessageBox.Show("设置中菜品后缀配置有误！");
                cb_suffix.Visibility = Visibility.Collapsed;
            }

            cb_xz.ItemsSource = App.MenuTypeBean.GetMenuTypes();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            string food_num = txt_food_num.Text;
            string food_name = txt_food_name.Text;
            string suffix = txt_suffix.Text;
            string taste = txt_taste.Text;
            string type = txt_type.Text;
            int position;
            float price;
            try
            {
                position = int.Parse(txt_position.Text);
                price = float.Parse(txt_price.Text);
            }
            catch
            {
                MessageBox.Show("位置或价格填写错误！只能是数字！");
                return;
            }
            string unit = txt_unit.Text;
            string text_enough = cb_text_enough.Text;
            string textprint_pos = cb_textprint_pos.Text;
            int menutype = ((MenuType)cb_xz.SelectedItem).Id;
            MyMenu myMenu = new MyMenu()
            {
                Food_name = food_name,
                Food_num = food_num,
                Price = price,
                Taste = taste,
                Type = type,
                Unit = unit,
                TextEnough = text_enough,
                Position = position,
                TextPrint_pos = textprint_pos,
                MenuType = menutype,
                Suffix = suffix
            };

            if (food_name == "" || food_num == "" || price.ToString() == "" || type == "" || unit == "" || position.ToString() == "")
            {
                MessageBox.Show("请将必要的信息填写完整！(口味选填)");
                return;
            }

            if (App.MyMenuBean.HasMyMenu(food_num))
            {
                MessageBox.Show("当前菜品编号已经存在！");
                return;
            }
            if (App.MyMenuBean.AddMyMenu(myMenu))
            {
                MessageBox.Show("添加成功！");
                all_MyMenu.Add(myMenu);
                this.Close();
            }
            else
            {
                MessageBox.Show("添加失败！");
            }
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void cb_food_unit_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            txt_unit.Text = cb_food_unit.SelectedValue+"";
        }

        private void cb_food_type_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            txt_type.Text = cb_food_type.SelectedValue + "";
        }
        private void cb_suffix_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            txt_suffix.Text = cb_suffix.SelectedValue + "";
        }
    }
}
